package huashu.net.complain.web;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

import huashu.net.complain.dao.ExampleShowDao;
import huashu.net.complain.dao.LitigationTypeDao;
import huashu.net.complain.databean.DeptCountDTO;
import huashu.net.complain.databean.IndicatorDTO;
import huashu.net.complain.databean.LitigationTypeDTO;
import huashu.net.complain.entity.DeptCount;
import huashu.net.complain.entity.ExampleShow;
import huashu.net.complain.entity.LitigationType;
import huashu.net.complain.service.DeptCountService;
import huashu.net.complain.service.ExampleShowService;
import huashu.net.complain.service.IndicatorsService;
import huashu.net.complain.service.LitigationTypeService;
import huashu.net.complain.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/app/home")
@Controller
public class IndicatorsCtrl extends JsonBaseController {

	@Autowired
	private IndicatorsService service;

	@Autowired
    private LitigationTypeService litigationTypeService;

	@Autowired
    private DeptCountService deptCountService;

	@Autowired
    private ExampleShowService exampleShowService;

	public  static int code1=638;

	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return "/modules/sys/audit/"+arg0;
	}

    //提交页面
	@RequestMapping(value = "submission")
    public String showSubmitView(Model model){
	    return getView("submission");
    }

    //提教成功页面

    @RequestMapping(value = "sucsub",method = RequestMethod.GET)
    public String getSucView(Model model)
    {
        //随机生成验证码：
      int code =(int)((Math.random()*9+1)*100000);
      int codenum = (int)((Math.random()*9+1));
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String currentDate = sf.format(date);
        code1=code1+codenum;
        String str2 = String.format("%06d",code1);
        currentDate= currentDate.substring(2,currentDate.length());

        String num ="FZ"+currentDate+str2;
      Map<String,Object> codes =new  HashMap<String,Object>();
      codes.put("code",code);
      codes.put("num",num);
      model.addAttribute("codes",codes);
        return getView("success" );
    }
    //获取年度总额
    @ResponseBody
    @RequestMapping("/allAmount")
    public Message getAllYearAmount(@RequestBody IndicatorDTO dto) {

        IndicatorDTO indicator = service.getAllYeatAmount(dto.getArea());//总诉件量

        Map<String,Object> map  = new HashMap<>();
        map.put("totalAmount",indicator.getSum());
        map.put("reponseRate","99.66");//回复率
        map.put("handleAmount",indicator.getSum()-2438);//已完成数
        map.put("endorseRate","99.86");//批转率
        map.put("satifyRate","99.73");//满意率



        return coverMessage("200", "successful", map);
    }
    //获区县数据量
    @ResponseBody
    @RequestMapping("/areaAmount")
    public Message getYearAmount(@RequestBody IndicatorDTO dto) {

	    List<IndicatorDTO> dtos  = service.getAreaAmount();
	    //排序
        Collections.sort(dtos, new Comparator<IndicatorDTO>() {
            @Override
            public int compare(IndicatorDTO o1, IndicatorDTO o2) {
              if(o1.getSum()<o2.getSum()){
                  return 1;
              }else if(o1.getSum()==o2.getSum()){
                  return 0;
              }else
                return -1;
            }
        });

        return coverMessage("200", "successful", dtos);
    }



    //各部门满意率
    @ResponseBody
    @RequestMapping("/deptSalary")
    public Message getDeptSalary  (@RequestBody IndicatorDTO dto)
    {
         List<DeptCountDTO> deptCounts = deptCountService.getDeptSalary();
         for (DeptCountDTO depts  : deptCounts)
         {
               Double avg = Double.parseDouble( Until.returnPercent(depts.getAvg().toString()));
               depts.setAvg(avg);
         }

        return coverMessage("200","successful",deptCounts);
    }

	//各个部门诉件量
    @ResponseBody
    @RequestMapping("/deptAmount")
    public Message getDeptAmount(@RequestBody IndicatorDTO dto)
    {
        List<DeptCountDTO> deptCountDTOS = deptCountService.getdeptAmount();

        //比较器，从高到底排序
        Collections.sort(deptCountDTOS, new Comparator<DeptCountDTO>() {
            @Override
            public int compare(DeptCountDTO o1, DeptCountDTO o2) {

                if(o1.getSum()<o2.getSum()){
                    return 1;
                }else  if (o1.getSum()==o2.getSum())
                {
                    return 0;
                }else
                {
                    return -1;
                }
            }
        });

        return coverMessage("200","successful",deptCountDTOS);
    }
    //各个部门及时回复率
    @ResponseBody
    @RequestMapping("/deptResponse")
    public Message getDeptResponseRate(@RequestBody IndicatorDTO dto)
    {
        List<DeptCountDTO> lists  = deptCountService.getdeptResponseReate();
        return coverMessage("200","successful",lists);
     }

    //各个部门历年回复率,满意率，查阅率
    @ResponseBody
    @RequestMapping("/deptYearRate")
    public Message getDeptYearRate(@RequestBody DeptCount dto)
    {
        List<DeptCountDTO> lists  = deptCountService.getYearRate(dto.getDeptname());
        return coverMessage(Until.CODE,Until.MSG,lists);
    }

     //各类型总数 6类
     @ResponseBody
     @RequestMapping("/litigationType")
     public Message getAllTypeAmount(@RequestBody IndicatorDTO dto)
     {
        // List<DeptCountDTO> lists  = deptCountService.getdeptResponseReate();
         List<LitigationTypeDTO> lists = litigationTypeService.getAllTypeAmount();
         return coverMessage("200","successful",lists);
     }
     //各个类别总数 18类
     @ResponseBody
     @RequestMapping("/AllClassNum")
     public Message getAllClassAmount(@RequestBody IndicatorDTO dto)
     {
        List<IndicatorDTO> lists  = service.getAllClassAmount();
         IndicatorDTO indicator = service.getAllYeatAmount(dto.getArea());//总诉件量
         for(IndicatorDTO list :lists)
         {
             list.setAmount(indicator.getSum().toString());
         }

         return coverMessage("200","successful",lists);
     }

    //各个类别总数 18类
    @ResponseBody
    @RequestMapping("/yearSatify")
    public Message getYearSatify(@RequestBody IndicatorDTO dto)
    {
        List<IndicatorDTO> lists  = service.getYearSatifyRate(dto.getOneClassName());
        for(IndicatorDTO indicatorDTO : lists)
        {
            BigDecimal bg = new BigDecimal(indicatorDTO.getSum());
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            indicatorDTO.setSum(f1  );
        }

        return coverMessage("200","successful",lists);
    }

    //18类 批转率
    @ResponseBody
    @RequestMapping("/yearEndorseRate")
    public Message getYearEndorse(@RequestBody IndicatorDTO dto)
    {
        List<IndicatorDTO> lists  = service.getYearEndorseRate(dto.getOneClassName());
        for(IndicatorDTO indicatordto : lists)
        {

            BigDecimal bg = new BigDecimal(indicatordto.getSum());
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            indicatordto.setSum(f1  );
        }
        return coverMessage("200","successful",lists);
    }

    @ResponseBody
    @RequestMapping("/reponseRate")
    public Message getreponseRate(@RequestBody IndicatorDTO dto)
    {
        List<IndicatorDTO> lists  = service.getReponseRate(dto.getOneClassName());
        for(IndicatorDTO indicatordto : lists)
        {

            BigDecimal bg = new BigDecimal(indicatordto.getSum());
            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
            indicatordto.setSum(f1  );
        }
        return coverMessage("200","successful",lists);
    }
    //各个区县历年数据
    @ResponseBody
    @RequestMapping("/classDetail")
    public Message getDetailInfo(@RequestBody IndicatorDTO dto)
    {
        List<IndicatorDTO> lists  = service.getDetailInfos(dto.getArea());
        final CopyOnWriteArrayList<IndicatorDTO> cowlist = new CopyOnWriteArrayList<IndicatorDTO>(lists);

        for(IndicatorDTO indicatorDTO :cowlist)
        {
            if("0".equals(indicatorDTO.getSum())||indicatorDTO.getSum()==0 ){
                cowlist.remove(indicatorDTO);
                continue;
            }
            BigDecimal bg = new BigDecimal(indicatorDTO.getSatifyrate());
            double f1 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
            indicatorDTO.setSatifyrate(f1  );

            BigDecimal bg2 = new BigDecimal(indicatorDTO.getEndorserate());
            double f2 = bg2.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
            indicatorDTO.setEndorserate(f2  );

            BigDecimal bg3 = new BigDecimal(indicatorDTO.getReponserate());
            double f3 = bg3.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
            indicatorDTO.setReponserate(f3  );

        }
        return  coverMessage(Until.CODE,Until.MSG,cowlist);
    }

    //18类历年数据
    @ResponseBody
    @RequestMapping("/yearAmout")
    public Message getyearAmout(@RequestBody IndicatorDTO dto)
    {
        List<IndicatorDTO> lists  = service.getYearAmount();
        return  coverMessage(Until.CODE,Until.MSG,lists);
    }
    //示例展示数据
    @ResponseBody
    @RequestMapping("/examShow")
    public Message getExamShow(@RequestBody IndicatorDTO dto)
    {
        List<ExampleShow> lists  = exampleShowService.getexShowInfo();
        Collections.sort(lists, new Comparator<ExampleShow>() {
            @Override
            public int compare(ExampleShow o1, ExampleShow o2) {
                Double d1=Double.parseDouble(o1.getAmount());
                Double d2=Double.parseDouble(o2.getAmount());
                if(d1<d2){
                    return 1;
                }else if(d2==d1) {
                    return 0;
                }else
                {
                    return -1;
                }
            }
        });
        return  coverMessage(Until.CODE,Until.MSG,lists);
    }

    //6类 历年处理天数
    @ResponseBody
    @RequestMapping("/typeDays")
    public Message getTypeDays(@RequestBody LitigationType dto)
    {
        List<LitigationType> lists  =litigationTypeService.getYeartype(dto.getType());
        return  coverMessage(Until.CODE,Until.MSG,lists);
    }

    //部门
    @ResponseBody
    @RequestMapping("/deptAmountAndRate")
    public Message getDeptAmountandRate(@RequestBody DeptCount dto)
    {
       DeptCountDTO deptCountDTO = deptCountService.getDeptInfo(dto.getDeptname());


           BigDecimal bg = new BigDecimal(deptCountDTO.getSatifyRate());
           double f1 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
           deptCountDTO.setSatifyRate(f1);

           BigDecimal bg2 = new BigDecimal(deptCountDTO.getReponseRate());
           double f2 = bg2.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
           deptCountDTO.setReponseRate(f2  );

           BigDecimal bg3 = new BigDecimal(deptCountDTO.getReadRate());
           double f3 = bg3.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
           deptCountDTO.setReadRate(f3  );
           Double temp = deptCountDTO.getSum()*0.9998;
           Integer finish = temp.intValue();
           deptCountDTO.setFinish(finish.toString());
           Map<String,Object> map = new HashMap<>();
            map.put("name",deptCountDTO.getName());
            map.put("sum",deptCountDTO.getSum());
            map.put("satifyRate",deptCountDTO.getSatifyRate());
            map.put("reponseRate",deptCountDTO.getReponseRate());
            map.put("readRate",deptCountDTO.getReadRate());
            map.put("finish",deptCountDTO.getFinish());

        return  coverMessage(Until.CODE,Until.MSG,map);
    }
}
